/*
'  DotNetNuke -  http://www.dotnetnuke.com
'  Copyright (c) 2002-2011
'  by DotNetNuke Corporation
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Text;
using System.Collections.Generic;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Framework;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.Mail;
using DotNetNuke.Modules.Store.Admin;
using DotNetNuke.Modules.Store.Customer;
using DotNetNuke.Modules.Store.Providers;
using DotNetNuke.Modules.Store.Providers.Address;

namespace DotNetNuke.Modules.Store.Cart
{
    public abstract class IPNPageBase : PageBase
    {
        private StoreInfo _storeSettings;

        public StoreInfo StoreSettings
        {
            get
            {
                if (_storeSettings == null)
                    _storeSettings = StoreController.GetStoreInfo(PortalSettings.PortalId);
                return _storeSettings;
            }
        }

        protected virtual OrderInfo updateOrderStatus(int orderID, OrderInfo.OrderStatusList orderStatus, string language)
        {
            // Update order status
            OrderController orderController = new OrderController();
            OrderInfo order = orderController.GetOrder(PortalSettings.PortalId, orderID);
            order.OrderStatusID = (int)orderStatus;
            order.Status = orderController.GetStatusText(order.OrderStatusID);
            orderController.UpdateOrder(order.OrderID, order.OrderDate, order.OrderNumber, order.ShippingAddressID, order.BillingAddressID, order.TaxTotal, order.ShippingCost, true, order.OrderStatusID, order.CustomerID);

            // Send order status change email
            sendOrderStatusChangeEmail(order);

            return order;
        }

        private void sendOrderStatusChangeEmail(OrderInfo Order)
        {
            // Store admin
            string adminEmail = "";
            string adminSubjectEmail = "";
            string adminBodyEmail = "";
            // Customer
            string customerEmail = "";
            string customerSubjectEmail = "";
            string customerBodyEmail = "";

            // Get Store Currency Symbol
            if (string.IsNullOrEmpty(StoreSettings.CurrencySymbol) == false)
                Order.CurrencySymbol = StoreSettings.CurrencySymbol;

            // Get Order Date Format
            string orderDateFormat = Localization.GetString("OrderDateFormat", this.LocalResourceFile);
            if (string.IsNullOrEmpty(orderDateFormat) == false)
                Order.DateFormat = orderDateFormat;

            // Get Customer Email
            IAddressProvider controler = StoreController.GetAddressProvider(StoreSettings.AddressName);
            IAddressInfo billingAddress = controler.GetAddress(Order.BillingAddressID);
            customerEmail = billingAddress.Email;

            // Get Admin Email
            adminEmail = StoreSettings.DefaultEmailAddress;

            // Customer Order Email Template
            customerSubjectEmail = Localization.GetString("CustomerStatusChangedEmailSubject", this.LocalResourceFile);
            customerBodyEmail = Localization.GetString("CustomerStatusChangedEmailBody", this.LocalResourceFile);

            // Admin Order Email Template
            adminSubjectEmail = Localization.GetString("AdminStatusChangedEmailSubject", this.LocalResourceFile);
            adminBodyEmail = Localization.GetString("AdminStatusChangedEmailBody", this.LocalResourceFile);

            // Init Email Order Replacement Tokens
            EmailOrderTokenReplace tkEmailOrder = new EmailOrderTokenReplace();
            tkEmailOrder.StoreSettings = StoreSettings;
            tkEmailOrder.Order = Order;

            // Replace tokens
            customerSubjectEmail = tkEmailOrder.ReplaceEmailOrderTokens(customerSubjectEmail);
            customerBodyEmail = tkEmailOrder.ReplaceEmailOrderTokens(customerBodyEmail);
            adminSubjectEmail = tkEmailOrder.ReplaceEmailOrderTokens(adminSubjectEmail);
            adminBodyEmail = tkEmailOrder.ReplaceEmailOrderTokens(adminBodyEmail);

            try
            {
                // Send Customer Email
                Mail.SendMail(adminEmail, customerEmail, "", customerSubjectEmail, customerBodyEmail, "", Mail.IsHTMLMail(customerBodyEmail) == true ? MailFormat.Html.ToString() : MailFormat.Text.ToString(), "", "", "", "");

                // Send Store Admin Email
                Mail.SendMail(adminEmail, adminEmail, "", adminSubjectEmail, adminBodyEmail, "", Mail.IsHTMLMail(adminBodyEmail) == true ? MailFormat.Html.ToString() : MailFormat.Text.ToString(), "", "", "", "");
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        protected virtual void AddEventLog(string logType, LogProperties properties, bool Bypass)
        {
            LogInfo logInfo = new LogInfo();
            logInfo.LogPortalID = PortalSettings.PortalId;
            logInfo.LogTypeKey = logType;
            logInfo.LogProperties = properties;
            logInfo.BypassBuffering = Bypass;
            EventLogController logControler = new EventLogController();
            logControler.AddLog(logInfo);
        }
    }
}
